package com.ruoyi.returnCustomer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.returnCustomer.domain.bo.TCustomerReturnRecordBo;
import com.ruoyi.returnCustomer.domain.vo.TCustomerReturnRecordVo;
import com.ruoyi.returnCustomer.domain.TCustomerReturnRecord;
import com.ruoyi.returnCustomer.mapper.TCustomerReturnRecordMapper;
import com.ruoyi.returnCustomer.service.ITCustomerReturnRecordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 客户退回记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-20
 */
@RequiredArgsConstructor
@Service
public class TCustomerReturnRecordServiceImpl implements ITCustomerReturnRecordService {

    private final TCustomerReturnRecordMapper baseMapper;

    /**
     * 查询客户退回记录
     */
    @Override
    public TCustomerReturnRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询客户退回记录列表
     */
    @Override
    public TableDataInfo<TCustomerReturnRecordVo> queryPageList(TCustomerReturnRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TCustomerReturnRecord> lqw = buildQueryWrapper(bo);
        Page<TCustomerReturnRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询客户退回记录列表
     */
    @Override
    public List<TCustomerReturnRecordVo> queryList(TCustomerReturnRecordBo bo) {
        LambdaQueryWrapper<TCustomerReturnRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TCustomerReturnRecord> buildQueryWrapper(TCustomerReturnRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TCustomerReturnRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCustomerId() != null, TCustomerReturnRecord::getCustomerId, bo.getCustomerId());
        lqw.eq(StringUtils.isNotBlank(bo.getBusType()), TCustomerReturnRecord::getBusType, bo.getBusType());
        lqw.eq(bo.getOperatorId() != null, TCustomerReturnRecord::getOperatorId, bo.getOperatorId());
        lqw.like(StringUtils.isNotBlank(bo.getOperatorName()), TCustomerReturnRecord::getOperatorName, bo.getOperatorName());
        lqw.eq(bo.getOperatorDeptId() != null, TCustomerReturnRecord::getOperatorDeptId, bo.getOperatorDeptId());
        lqw.eq(bo.getReturnDeptId() != null, TCustomerReturnRecord::getReturnDeptId, bo.getReturnDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getReturnReason()), TCustomerReturnRecord::getReturnReason, bo.getReturnReason());
        lqw.like(StringUtils.isNotBlank(bo.getCustomerName()), TCustomerReturnRecord::getCustomerName, bo.getCustomerName());
        lqw.eq(StringUtils.isNotBlank(bo.getTel()), TCustomerReturnRecord::getTel, bo.getTel());
        return lqw;
    }

    /**
     * 新增客户退回记录
     */
    @Override
    public Boolean insertByBo(TCustomerReturnRecordBo bo) {
        TCustomerReturnRecord add = BeanUtil.toBean(bo, TCustomerReturnRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改客户退回记录
     */
    @Override
    public Boolean updateByBo(TCustomerReturnRecordBo bo) {
        TCustomerReturnRecord update = BeanUtil.toBean(bo, TCustomerReturnRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TCustomerReturnRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除客户退回记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
